package com.github.springcloud.gateway.filter;

import com.github.springcloud.gateway.config.WenziIgnorePropertiesConfig;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

/**
 * Created by zhaozuowen on 2018/7/13.
 */
@Component
public class ApiGatewayZuulFilter extends ZuulFilter {

    @Autowired
    private WenziIgnorePropertiesConfig config;




    @Autowired
    private  AntPathMatcher antPathMatcher;

    private Logger logger= LoggerFactory.getLogger(ApiGatewayZuulFilter.class);
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {

        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();

        for(String url:config.getUrls()){

            if(antPathMatcher.match(url,request.getRequestURI())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext requestContext= RequestContext.getCurrentContext();

        HttpServletRequest request=requestContext.getRequest();

        logger.info("当前请求的地址是："+request.getRequestURI()+"请求方式是:"+request.getMethod());


        return null;
    }
}
